//
//  MyAllOrderListVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/11.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "MyAllOrderListVC.h"
#import "MyOrderSectionModel.h"
#import "MyOrderNumberCell.h"
#import "MyOrderBottomCell.h"
#import "MyOrderGoodsItemCell.h"
#import "GoodsEvaluateVC.h"
#import "BaseNavigationController.h"
#import "GoodEvaluateParm.h"
#import "ApplyForRefundVC.h"
#import "WaitBuyderRefundVC.h"
#import "WaitSalerGetGoodsVC.h"
#import "ApplyRefundTwoVC.h"
#import <WXApi.h>
#import <AlipaySDK/AlipaySDK.h>
@interface MyAllOrderListVC ()<MyOrderListDelegate,MyOrderBottomCellDelegate>

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<MyOrderSectionModel *> *dataModels;


@property (nonatomic, strong) NSString *payType;


/** <#note#> */
@property (nonatomic, strong) MyOrderSectionModel *payModel;
@end

@implementation MyAllOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.groupTableView.backgroundColor = viewBgGrey;
    
    self.groupTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.groupTableView.estimatedRowHeight = 200;
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyOrderNumberCell class]) bundle:nil] forCellReuseIdentifier:[MyOrderNumberCell identifierStr]];
    
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyOrderBottomCell class]) bundle:nil] forCellReuseIdentifier:[MyOrderBottomCell identifierStr]];

    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyOrderGoodsItemCell class]) bundle:nil] forCellReuseIdentifier:[MyOrderGoodsItemCell identifierStr]];



    
//
//    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyHouseInfoBottomCell class]) bundle:nil] forCellReuseIdentifier:[MyHouseInfoBottomCell identifierStr]];
    
    
    
    MJWeakSelf
    self.groupTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.groupTableView.mj_header beginRefreshing];
    
    
    self.groupTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
    }];
    
   
    CGFloat navH = IPhoneX ? 88 : 64;
    CGFloat tabbarH = IPhoneX ? 83 : 49;
    
    if (@available(iOS 11.0, *)) {
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(XMGTitlesViewH, 0, tabbarH, 0);
        
    }else{
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(navH + XMGTitlesViewH, 0, tabbarH,0 );
        
        
    }
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidSuccessNotification) name:WXApiDidSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidFailNotification) name:WXApiDidFailNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidSuccessNotification) name:AlipayDidSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidFailNotification) name:AlipayDidFailNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orderDidClose) name:@"OrderDidClose" object:nil];

    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didReceiveSureReceiveGoodBtnDidTapNSNotification) name:@"SureReceiveGoodBtnDidTap" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didSusseceEvauate) name:@"DidSusseceEvauate" object:nil];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(orderDidDelete) name:@"OrderDidDelete" object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didPostWuLiuInfor) name:@"DidPostWuLiuInfor" object:nil];


    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didApplyForRefundSuccsece) name:@"didApplyForRefundSuccsece" object:nil];
    

}


- (void)didPostWuLiuInfor{
    
    
    if (self.status == nil || [self.status isEqualToString:@"1"]) {
        [self loadData];
    } else {
        
    }
    
    
    
}


- (void)orderDidDelete{
    
    if (self.status == nil) {
        [self loadData];
    }
    
    
}


- (void)didApplyForRefundSuccsece{
    
    
    if (self.status == nil|| [self.status isEqualToString:@"1"]  || [self.status isEqualToString:@"2"]  || [self.status isEqualToString:@"4"]) {
        
        [self loadData];
        
    } else {
        
    }
    
}

- (void)orderDidClose{
    
    if (self.status == nil || [self.status isEqualToString:@"0"]) {
        [self loadData];
    } else {
        
    }
    
    
}

- (void)didSusseceEvauate{
    
    NSLog(@"%@+++++++++++++++",self.status);

    if (self.status == nil || [self.status isEqualToString:@"3"]) {
        NSLog(@"%@$$$$$$$$$$$$",self.status);

        [self loadData];
    } else {
        
    }
    
    
    
}


- (void)WXApiDidSuccessNotification{
    
    
    if (self.status == nil || [self.status isEqualToString:@"0"] || [self.status isEqualToString:@"1"]) {
        [self loadData];
    } else {
        
    }
    
}

- (void)WXApiDidFailNotification{
    
    [SVProgressHUD showInfoWithStatus:@"支付失败!"];
    
}

- (void)AlipayDidSuccessNotification{
    
    
    
    if (self.status == nil || [self.status isEqualToString:@"0"] || [self.status isEqualToString:@"1"]) {
        [self loadData];
    } else {
        
    }
    
}


- (void)AlipayDidFailNotification{
    
    [SVProgressHUD showInfoWithStatus:@"支付失败!"];

    
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}










- (void)didReceiveSureReceiveGoodBtnDidTapNSNotification{
    
    NSLog(@"%@+++++++++++++++",self.status);
    if (self.status == nil || [self.status isEqualToString:@"2"] || [self.status isEqualToString:@"3"]) {
        NSLog(@"%@$$$$$$$$$$$$",self.status);

        [self loadData];
    } else {
        
    }
  
}



- (void)loadData{
    
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"uid"] = myViewModel.user.uid;
//    parameters[@"page"] = @"2";

    if (self.status != nil) {
        parameters[@"status"] = self.status;

    }
    
        //        0 没退货正常状态1退款中5退款成功refund_satatus
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/myOrderList" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
                        MYJSONSTR
            //            _model.lastString
          //[[responseObject[@"data"][@"data"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            weakSelf.dataModels = [MyOrderSectionModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"order_list"]];
            
//            for (MyOrderSectionModel *temp in weakSelf.dataModels) {
//                NSLog(@"%@",temp.order_status);
//            }
            
          
            
            [weakSelf.groupTableView.mj_footer resetNoMoreData];
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                
            } else {
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
            }
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            
            
            [weakSelf.groupTableView reloadData];
            
            
        } else {
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
    
    
}





- (void)loadMoreData{
    
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"uid"] = myViewModel.user.uid;
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    if (self.status != nil) {
        parameters[@"status"] = self.status;
        
    }
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/myOrderList" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            NSMutableArray<MyOrderSectionModel *> *temp = [MyOrderSectionModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"order_list"]];
            
            
//            for (MyOrderSectionModel *temp9 in temp) {
//                NSLog(@"%@",temp9.order_status);
//            }
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            

            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.groupTableView.mj_footer endRefreshing];
                
                
            }
            
            
            [weakSelf.groupTableView reloadData];
            
        }else{
            
            [weakSelf.groupTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return 0;
        
    } else {
        return self.dataModels.count;
        
    }
    
    
}
//4 5 -1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.dataModels == nil) {
        return 0;
        
    } else {
        
        MyOrderSectionModel *model = self.dataModels[section];
        
        if ([model.order_status isEqualToString:@"-1"]) {//退款
            return model.order_item_list.count + 2;

        } else if ([model.order_status isEqualToString:@"0"]){//待付款
            return model.order_item_list.count + 2;

        }else if ([model.order_status isEqualToString:@"1"]){//待发货
            return model.order_item_list.count + 2;
            
        }else if ([model.order_status isEqualToString:@"2"]){//已发货
            return model.order_item_list.count + 2;
            
        }else if ([model.order_status isEqualToString:@"3"]){//已收货
            
            return model.order_item_list.count + 2;
            
        }else if ([model.order_status isEqualToString:@"5"]){//已关闭
            
            return model.order_item_list.count + 2;

        }else{//已完成4
             return 0;
        }
        
        
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyOrderSectionModel *model = self.dataModels[indexPath.section];

    if ([model.order_status isEqualToString:@"-1"]) {//退款
        
        if (indexPath.row == 0) {
            MyOrderNumberCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderNumberCell identifierStr]];
            
            tableCell.model = model;
            
            return tableCell;
            
        } else if (indexPath.row == model.order_item_list.count + 1){
            
            MyOrderBottomCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderBottomCell identifierStr]];
            
            tableCell.model = model;
            
            
            tableCell.index = indexPath;

            tableCell.delegate = self;

            
            return tableCell;
        }else{
            
            MyOrderGoodsItemCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderGoodsItemCell identifierStr]];
            
            if (model.order_item_list[indexPath.row - 1].order_status == nil) {
                
                model.order_item_list[indexPath.row - 1].order_status = model.order_status;

            }
            
            tableCell.model = model.order_item_list[indexPath.row - 1];
            tableCell.index = indexPath;

            
            tableCell.delegate = self;

            return tableCell;
            
        }
       
        
    } else if ([model.order_status isEqualToString:@"0"]){//待付款
        if (indexPath.row == 0) {
            MyOrderNumberCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderNumberCell identifierStr]];
            
            tableCell.model = model;

            return tableCell;
        } else if (indexPath.row == model.order_item_list.count + 1){
            
            MyOrderBottomCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderBottomCell identifierStr]];
            
            tableCell.model = model;
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
        }else{
            
            MyOrderGoodsItemCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderGoodsItemCell identifierStr]];
            if (model.order_item_list[indexPath.row - 1].order_status == nil) {
                
                model.order_item_list[indexPath.row - 1].order_status = model.order_status;
                
            }
            tableCell.model = model.order_item_list[indexPath.row - 1];
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
            
        }
        
    }else if ([model.order_status isEqualToString:@"1"]){//待发货
        
        if (indexPath.row == 0) {
            MyOrderNumberCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderNumberCell identifierStr]];
            
            tableCell.model = model;

            return tableCell;
        } else if (indexPath.row == model.order_item_list.count + 1){
            
            MyOrderBottomCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderBottomCell identifierStr]];
            
            tableCell.model = model;
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
        }else{
            
            MyOrderGoodsItemCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderGoodsItemCell identifierStr]];
            if (model.order_item_list[indexPath.row - 1].order_status == nil) {
                
                model.order_item_list[indexPath.row - 1].order_status = model.order_status;
                
            }
            tableCell.model = model.order_item_list[indexPath.row - 1];
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
            
        }
        
    }else if ([model.order_status isEqualToString:@"2"]){//已发货
        if (indexPath.row == 0) {
            MyOrderNumberCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderNumberCell identifierStr]];
            tableCell.model = model;

            
            return tableCell;
        }else if (indexPath.row == model.order_item_list.count + 1){
            
            MyOrderBottomCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderBottomCell identifierStr]];
            
            tableCell.model = model;
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
        }else{
            
            MyOrderGoodsItemCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderGoodsItemCell identifierStr]];
            
            if (model.order_item_list[indexPath.row - 1].order_status == nil) {
                
                model.order_item_list[indexPath.row - 1].order_status = model.order_status;
                
            }
            tableCell.model = model.order_item_list[indexPath.row - 1];
            tableCell.delegate = self;
            tableCell.index = indexPath;

            
            return tableCell;
            
        }
        
    }else if ([model.order_status isEqualToString:@"3"]){//已收货
        if (indexPath.row == 0) {
            MyOrderNumberCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderNumberCell identifierStr]];
            
            tableCell.model = model;

            return tableCell;
        }else if (indexPath.row == model.order_item_list.count + 1){
            
            MyOrderBottomCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderBottomCell identifierStr]];
            
            tableCell.model = model;
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
        }else{
            
            MyOrderGoodsItemCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderGoodsItemCell identifierStr]];
            
            if (model.order_item_list[indexPath.row - 1].order_status == nil) {
                
                model.order_item_list[indexPath.row - 1].order_status = model.order_status;
                
            }
            tableCell.model = model.order_item_list[indexPath.row - 1];
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
            
        }
        
    }else if ([model.order_status isEqualToString:@"5"]){//已关闭
        if (indexPath.row == 0) {
            
            MyOrderNumberCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderNumberCell identifierStr]];
            
            tableCell.model = model;

            return tableCell;
            
        }else if (indexPath.row == model.order_item_list.count + 1){
            
            MyOrderBottomCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderBottomCell identifierStr]];
            
            tableCell.model = model;
            tableCell.delegate = self;
            tableCell.index = indexPath;

            return tableCell;
            
        }else{
            
            
            MyOrderGoodsItemCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MyOrderGoodsItemCell identifierStr]];
            
            if (model.order_item_list[indexPath.row - 1].order_status == nil) {
                
                model.order_item_list[indexPath.row - 1].order_status = model.order_status;
                
            }
            
            tableCell.index = indexPath;
            
            tableCell.model = model.order_item_list[indexPath.row - 1];
            tableCell.delegate = self;

            return tableCell;
            
        }
        
    }else{//已完成4
        
        return [[UITableViewCell alloc]init];
        
    }



    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    if (section == 0) {
        return 0.00001;
    } else {
        return 12;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.00001;
}




- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *temp = [[UIView alloc]init];
    
    temp.backgroundColor = viewBgGrey;
    
    return temp;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    
    return [[UIView alloc]init];
}


//if ([_model.refund_status isEqualToString:@"0"]) {
//    [self.rightBtn setTitle:@"退款/退货" forState:UIControlStateNormal];
//    [self.rightBtn setTitle:@"退款/退货" forState:UIControlStateHighlighted];
//} else if ([_model.refund_status isEqualToString:@"1"]){
//    [self.rightBtn setTitle:@"买家申请退款" forState:UIControlStateNormal];
//    [self.rightBtn setTitle:@"买家申请退款" forState:UIControlStateHighlighted];
//} else if ([_model.refund_status isEqualToString:@"2"]){
//    [self.rightBtn setTitle:@"等待买家退货" forState:UIControlStateNormal];
//    [self.rightBtn setTitle:@"等待买家退货" forState:UIControlStateHighlighted];
//} else if ([_model.refund_status isEqualToString:@"3"]){
//    [self.rightBtn setTitle:@"等待卖家确认收货" forState:UIControlStateNormal];
//    [self.rightBtn setTitle:@"等待卖家确认收货" forState:UIControlStateHighlighted];
//} else if ([_model.refund_status isEqualToString:@"4"]){
//    [self.rightBtn setTitle:@"卖家已收货" forState:UIControlStateNormal];
//    [self.rightBtn setTitle:@"卖家已收货" forState:UIControlStateHighlighted];
//}else if ([_model.refund_status isEqualToString:@"5"]){
//    [self.rightBtn setTitle:@"退款已成功" forState:UIControlStateNormal];
//    [self.rightBtn setTitle:@"退款已成功" forState:UIControlStateHighlighted];
//}else{
- (void)btnDidClickWith:(NSString *)title index:(NSIndexPath *)index{
    


    if ([title isEqualToString:@"我要评价"]) {
        [self goToEvaluateWithIndx:index];
    } else if ([title isEqualToString:@"退款/退货"]) {
        [self applyForRefundWithIndx:index];
    }else if ([title isEqualToString:@"买家申请退款"]) {
        
        [self applyTwoVCWithIndx:index];
        
    }else if ([title isEqualToString:@"等待买家退货"]) {
        
        [self waitBuyderRefundGoodsWithIndx:index];
        
    }else if ([title isEqualToString:@"等待卖家确认收货"]) {
        
        [self waitSalerGetGoodsVCWithIndx:index];
        
    }else if ([title isEqualToString:@"卖家已收货"]) {
        
    }else if ([title isEqualToString:@"去支付"]) {
        
    }
    
    
    
}


- (void)waitSalerGetGoodsVCWithIndx:(NSIndexPath *)index{
    
    MyOrderSectionModel *secModel = self.dataModels[index.section];
    MyOrderItemsModel *subModel = secModel.order_item_list[index.row - 1];
    
    WaitSalerGetGoodsVC *temp = [[WaitSalerGetGoodsVC alloc]init];
    
    temp.parmModel.order_id = secModel.order_id;
    
    temp.parmModel.order_goods_id = subModel.order_goods_id;
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:temp];
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
}

- (void)waitBuyderRefundGoodsWithIndx:(NSIndexPath *)index{
    
    
    MyOrderSectionModel *secModel = self.dataModels[index.section];
    MyOrderItemsModel *subModel = secModel.order_item_list[index.row - 1];
    
    WaitBuyderRefundVC *temp = [[WaitBuyderRefundVC alloc]init];
    
    temp.parmModel.order_id = secModel.order_id;
    
    temp.parmModel.order_goods_id = subModel.order_goods_id;
    
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:temp];
    
    [self presentViewController:nav animated:YES completion:nil];
    
}


- (void)applyTwoVCWithIndx:(NSIndexPath *)index{
    
    MyOrderSectionModel *secModel = self.dataModels[index.section];
    MyOrderItemsModel *subModel = secModel.order_item_list[index.row - 1];
    
    ApplyRefundTwoVC *temp = [[ApplyRefundTwoVC alloc]init];
    
    temp.parmModel.order_id = secModel.order_id;
    
    temp.parmModel.order_goods_id = subModel.order_goods_id;
    
    temp.parmModel.placehoder = subModel.price;
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:temp];
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
//
}


- (void)applyForRefundWithIndx:(NSIndexPath *)index{
    
    MyOrderSectionModel *secModel = self.dataModels[index.section];
    MyOrderItemsModel *subModel = secModel.order_item_list[index.row - 1];
    
    ApplyForRefundVC *temp = [[ApplyForRefundVC alloc]init];
    
    temp.parmModel.order_id = secModel.order_id;
    
    temp.parmModel.order_goods_id = subModel.order_goods_id;
    
    temp.parmModel.placehoder = subModel.price;
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:temp];
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
    
    
}



-(void)goToEvaluateWithIndx:(NSIndexPath *)index{
    
    /**
     * 评论商品
     * uid                 会员uid
     * order_id            订单id
     * order_no            订单编号
     * order_goods_id      订单内商品id
     * content             评论内容
     * scores              分值 1-5
     * explain_type        1好评2中评3差评
     * is_anonymous        是否匿名评价  0不是 1是匿名评价
     http://183.95.85.31:8001/index.php/api/Good/addGoodsEvaluate

     */
    
    MyOrderSectionModel *secModel = self.dataModels[index.section];
    MyOrderItemsModel *subModel = secModel.order_item_list[index.row - 1];
    
    GoodsEvaluateVC *evaluVC = [[GoodsEvaluateVC alloc]init];
    
    GoodEvaluateParm *model = [[GoodEvaluateParm alloc]init];
    
    model.uid = myViewModel.user.uid;
    
    model.order_id = secModel.order_id;
    
    model.order_no = secModel.order_no;
    
    model.order_goods_id = subModel.order_goods_id;
    
    model.imgUrl = subModel.pic_cover_small;
    
    model.price = subModel.price;
    
    model.good_name = subModel.goods_name;
    
    model.explain_type = @"1";
    
    model.is_anonymous = @"0";
    
    evaluVC.parmar = model;
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:evaluVC];
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
}







- (void)goToPay{
    
    MJWeakSelf;
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:@"选择支付方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [contr addAction:action0];
    
    
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"微信支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        weakSelf.payType = @"1";
        
        [weakSelf wachatPay];
        
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"支付宝支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        weakSelf.payType = @"2";
        
        [weakSelf alipayPayWith];
        
    }];
    
    
    
    [contr addAction:action1];
    [contr addAction:action2];
    
    
    [self presentViewController:contr animated:YES completion:nil];
    
    
    
    
}





- (void)alipayPayWith{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"payment"] = self.payModel.pay_rmb;
    parameters[@"out_trade_no"] = self.payModel.out_trade_no;
    parameters[@"pay_type"] = self.payType;
    
    [SVProgressHUD showWithStatus:@"订单确认中..."];
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Pay/pay_order" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
            [[AlipaySDK defaultService] payOrder:responseObject[@"data"] fromScheme:@"com.beisheng.feifubao" callback:^(NSDictionary *resultDic) {
                
                
                //                NSLog(@"reslut = %@",resultDic);
                
            }];
            
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
        
    }];
    
    
    
}


- (void)wachatPay{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"payment"] = self.payModel.pay_rmb;
    parameters[@"out_trade_no"] = self.payModel.out_trade_no;
    parameters[@"pay_type"] = self.payType;
    
    [SVProgressHUD showWithStatus:@"订单确认中..."];
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Pay/pay_order" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
            //            MYJSONSTR;
            
            [self WXPayWithDic:responseObject[@"data"]];
            
            
        } else {
            
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
        
    }];
    
    
    
}




- (void)WXPayWithDic:(NSDictionary *)dic{
    
    //需要创建这个支付对象
    PayReq *req   = [[PayReq alloc] init];
    //由用户微信号和AppID组成的唯一标识，用于校验微信用户
    req.openID = dic[@"appid"];
    
    // 商家id，在注册的时候给的
    req.partnerId = dic[@"partnerid"];
    
    // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给你们服务器的，你们服务器再传给你
    req.prepayId  = dic[@"prepayid"];
    
    // 根据财付通文档填写的数据和签名
    //这个比较特殊，是固定的，只能是即req.package = Sign=WXPay
    req.package   = dic[@"pkg"];
    
    // 随机编码，为了防止重复的，在后台生成
    req.nonceStr  = dic[@"noncestr"];
    
    // 这个是时间戳，也是在后台生成的，为了验证支付的
    NSString * stamp = dic[@"timestamp"];
    
    req.timeStamp = stamp.intValue;
    
    // 这个签名也是后台做的
    req.sign = dic[@"sign"];
    
    //发送请求到微信，等待微信返回onResp
    [WXApi sendReq:req];
}





- (void)bottomBtnDidClickWith:(NSString *)title indexPath:(NSIndexPath *)index
{
    
    MyOrderSectionModel *model = self.dataModels[index.section];
    self.payModel = model;
//    MyOrderItemsModel *subModel = model.order_item_list[index.row - 1];
    if ([title isEqualToString:@"确认收货"]) {
        
        [self sureReceiveGood:model.order_id];
        
    } else if ([title isEqualToString:@"去支付"]){
        
        [self goToPay];
        
    }else if ([title isEqualToString:@"关闭订单"]){
        
        [self closeOrder];
        
    }else if ([title isEqualToString:@"删除订单"]){
        
        [self deletOrder];
        
    }else if ([title isEqualToString:@"确认收货"]){
        
        
    }else if ([title isEqualToString:@"确认收货"]){
        
        
    }else if ([title isEqualToString:@"确认收货"]){
        
        
    }
    
    
    
    
}


- (void)closeOrder{
    
    /**
     *  关闭订单
     *  order_id    订单id
     
     http://183.95.85.31:8001/index.php/api/Order/orderClose

     */
    
    
    
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:@"确定关闭订单吗?" preferredStyle:UIAlertControllerStyleAlert];
    
    MJWeakSelf;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
     
        [weakSelf  closeOrderLast];
        
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    
    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
    
    
}



- (void)deletOrder{
    
    
    
    /**
     *  关闭订单
     *  order_id    订单id
     
     http://183.95.85.31:8001/index.php/api/Order/orderClose
     
     */
    
    
    
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:@"确定删除订单吗?" preferredStyle:UIAlertControllerStyleAlert];
    
    MJWeakSelf;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        
        [weakSelf  deletOrderLast];
        
    }];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    
    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
   
}





- (void)deletOrderLast{
    
    [SVProgressHUD showWithStatus:@"正在删除..."];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"order_id"] = self.payModel.order_id;
    
    parameters[@"uid"] = myViewModel.user.uid;
    
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/deleteOrder" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"OrderDidDelete" object:nil];
            
        }else{
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
    }];
    
}







- (void)closeOrderLast{
    
    
    [SVProgressHUD showWithStatus:@"正在关闭..."];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"order_id"] = self.payModel.order_id;
    
    
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/orderClose" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"OrderDidClose" object:nil];
            
        }else{
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
    }];
    
    
    
}

- (void)sureReceiveGood:(NSString *)orderId{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"uid"] = myViewModel.user.uid;
    parameters[@"order_id"] = orderId;
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"SureReceiveGoodBtnDidTap" object:nil];
//    return;
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Order/orderTakeDelivery" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
        
            
            [weakSelf loadData];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"SureReceiveGoodBtnDidTap" object:nil];
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
        
        
        
    }];
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
